package sort

/**
 * Created by SEELE on 2017/9/25.
 */
fun main(args: Array<String>) {

    val num = arrayOf(3, 1, 0, 10, 9, 100, 34, 5, 45)

    bubbleSort(num)

    println(num.toList())

}

fun bubbleSort(num: Array<Int>) {

    if (num.size <= 1)
        return

    val size = num.size

    for (i in size - 1 downTo 0) {


        for (j in 0 until i) {

            if (num[j + 1] < num[j])
                swap(num, j + 1, j)
        }

    }
}

fun swap(num: Array<Int>, i: Int, j: Int) {

    num[i] = num[i] + num[j]
    num[j] = num[i] - num[j]
    num[i] = num[i] - num[j]
}